const config = require('../../../utils/config.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    status:0,
    pageNum: 1,
    pageSize: 10,
    isLastPage: false,
    more: '正在加载'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._getOrder()
  },
  _getOrder() {
    let _this = this
    wx.request({
      url: config.HOST + '/productOrder/list',
      data: {
        status: this.data.status,
        pageNum:this.data.pageNum++,
        pageSize:this.data.pageSize,
        token: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res.data.data.list)
        if (res.data.status == 200) {
          let more = ''
          if (res.data.data.isLastPage == true) {
            more = "我也是有底线的"
          } else {
            more = "正在加载"
          }
          res.data.data.list.forEach((item,index)=>{
            item.img = item.product.imgs.split(',')[0]
          })
          _this.data.orders = _this.data.orders.concat(res.data.data.list)
          _this.setData({
            isLastPage: res.data.data.isLastPage,
            orders: _this.data.orders,
            more:more
          })

          wx.stopPullDownRefresh()
        }
      }
    })
  },
  onChange(e) {
    console.log(e.detail)
    // 0 等待交易 1交易中 2交易成功 3交易失败 4用户取消交易
    this.data.pageNum = 1
    this.setData({
      orders: [],
      status:e.detail.index
    })
    this._getOrder()
  },
  //进入订单详细
  orderDetail(event) {
    console.log(event.currentTarget.dataset.index)
    app.globalData.productOrderDetail = this.data.orders[event.currentTarget.dataset.index]
    wx.navigateTo({
      url: '../productOrderDetail/index',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.data.pageNum = 1
    this.setData({
      orders: []
    })
    this._getOrder()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isLastPage) {
      return;
    }
    this._getOrder()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})